//
//  LMWebViewCookieManager.m
//  LMWebViewDemo
//
//  Created by mengmenglu on 5/16/16.
//  Copyright © 2016 Hangzhou TaiXuan Network Technology Co., Ltd. All rights reserved.
//

#import "LMWebViewCookieManager.h"

@implementation LMWebViewCookieManager

+ (instancetype)shareManager {
    
    static LMWebViewCookieManager *_shraeManager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shraeManager = [[self alloc] init];
    });
    
    return _shraeManager;
}


#pragma mark 设置cookie
- (void)setCookieWithDomain:(NSString *)domain path:(NSString *)path key:(NSString *)key value:(NSString *)value {
    
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    if (domain) {
        [mutableDict setObject:domain forKey:NSHTTPCookieDomain];
    }
    
    if (path) {
        [mutableDict setObject:path forKey:NSHTTPCookiePath];
    }
    
    if (key) {
        [mutableDict setObject:key forKey:NSHTTPCookieName];
    }
    
    if (value) {
        [mutableDict setObject:value forKey:NSHTTPCookieValue];
    }
    [mutableDict setObject:@"0" forKey:NSHTTPCookieVersion];
    
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:mutableDict];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    
}


#pragma mark 清空cookie
- (void)clearAllCookie {
    
    // 清除cookie内存中的cookie
    NSHTTPCookieStorage *cookieStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *cookie in [cookieStore cookies]) {
        [cookieStore deleteCookie:cookie];
    }
    
    // 清除缓存中的cookie
    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
    [NSURLCache setSharedURLCache:sharedCache];
    
    
    //清除UIWebView的缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
}



@end
